package com.zqh.volatileDemo;

import java.util.concurrent.TimeUnit;

/**
 * @author 曾庆红
 * {@code @date} 2023-10-12 10:11
 * 锁定某对象o，如果o的属性发生变化，不影响锁的使用
 * 但是如果o变成另一个对象，则锁定的对象发生改变
 */

public class T2 {
    Object o = new Object();
    void m(){
        synchronized (o){
            while(true){
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            }
        }
    }
    public static void main(String[] args){
        T2 t = new T2();
        new Thread(t::m,"t1").start();
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Thread t2 = new Thread(t::m,"t2");
        //t.o = new Object();
        t2.start();
    }
}
